﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Model;
using RM2012Server.Controller;
using RM2012Server.Business;

namespace RM2012Server.Component
{
    public partial class frmProfile : Office2007Form
    {
        #region Fields
        UserCtrl m_UserCtrl = new UserCtrl();
        #endregion

        #region Constructor
        public frmProfile()
        {
            InitializeComponent();
        }
        #endregion
        

        private void frmProfile_Load(object sender, EventArgs e)
        {
            string userName = GeneralInfo.userName;
            string userId = m_UserCtrl.GetIdByUsername(userName);
            DataTable currentInfo = m_UserCtrl.GetById(userId);
            if (currentInfo.Rows.Count > 0)
            {
                txtName.Text = currentInfo.Rows[0]["FullName"].ToString();
                txtAddress.Text = currentInfo.Rows[0]["Address"].ToString();
                txtPhone.Text = currentInfo.Rows[0]["Phone"].ToString();
                txtEmail.Text = currentInfo.Rows[0]["Email"].ToString();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UserInfo m_info = new UserInfo();
            m_info.UserId = m_UserCtrl.GetIdByUsername(GeneralInfo.userName);
            m_info.FullName = txtName.Text;
            m_info.Address = txtAddress.Text;
            m_info.Phone = txtPhone.Text;
            m_info.Email = txtEmail.Text;
            if (m_UserCtrl.UpdateProfile(m_info) > 0)
            {
                MessageBoxEx.Show("Cập nhật thành công !", "CẬP NHẬT", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
            else
            {
                MessageBoxEx.Show("Không thể cập nhật ! Vui lòng kiểm tra lại kết nối !", "CẬP NHẬT", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
